//
//  LayouView.swift
//  weibo1
//
//  Created by Apple on 15/12/1.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
private let Offset :CGFloat = 5
class LayouView: UICollectionView {
    //设置配图的属性
    var imageURL: [NSURL]? {
        didSet {
            
            if imageURL != nil {
                label.text = "\(imageURL!.count)"
            }
            let psize = printFarm()
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(psize)
            }
            
            //刷新列表
            reloadData()
        }
    }
    
    
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let layouts = UICollectionViewFlowLayout()
        layouts.minimumInteritemSpacing = Offset
        layouts.minimumLineSpacing = Offset
    
        super.init(frame: CGRectZero, collectionViewLayout: layouts)
        backgroundColor = UIColor.randomColor()
        setupUI()
        //设置数据源
        dataSource = self
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func printFarm() -> CGSize {
        
        
        
        let imageCount = imageURL?.count ?? 0
        //图片的最大宽度
        let maxW = ScreenW - 2 * OffsetCell
        //每一行三张图片的时候每一张的宽度
        let itemW = (maxW - 2 * Offset) / 3.002
        
        //设置itmesize
        let layou = collectionViewLayout as! UICollectionViewFlowLayout
        layou.itemSize = CGSizeMake(itemW, itemW)
        
        
        if imageCount == 0 {
            return CGSizeZero
        }
        
        if imageCount == 1{
            let imagesize = CGSize(width: 180, height: 120)
            layou.itemSize = imagesize
            return imagesize
        }
        
        if imageCount == 4{
            let W = 2.001 * itemW + Offset
            return CGSize(width: W, height: W)
        }
        /*
        1,2,3
        4,5,6
        7,8,9
        
        */
        //求出一共有多少行
        let row = CGFloat((imageCount - 1) / 3 + 1)
        //h = 每一个高度 * 多行 + 间局
        return CGSizeMake(maxW, itemW * row + Offset * (row - 1))
        
    }

    
    private func setupUI(){
        
        addSubview(label)
        label.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.center)
        }
        //注册cell
        registerClass(PictureCell.self, forCellWithReuseIdentifier: "cell")
        
    }

    //测试label
    private var label :UILabel = {
        let lab = UILabel()
        lab.text = ""
        lab.textColor = UIColor.redColor()
        return lab
        }()
    
    
}
//MARK: 数据源方法
extension LayouView :UICollectionViewDataSource{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageURL?.count ?? 0
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! PictureCell
        
        cell.imageurl = imageURL![indexPath.row]
        return cell
    }
    
}

//MARK:自定义cell
class PictureCell :UICollectionViewCell{
    var imageurl: NSURL? {
        didSet{
            
            iconImage.sd_setImageWithURL(imageurl)
        }
        
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        contentView.addSubview(iconImage)
        iconImage.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
            
        }
    }
    
    private lazy var iconImage: UIImageView = UIImageView()
    
}
